package com.monas.client.dialog;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.monas.client.util.Node;
import com.monas.client.util.UIHelper;
import com.monas.client.view.MainView;

public class AbstractNodeDialog extends TitleAreaDialog {

	/**
	 * Button untuk OK.
	 */
	protected Button ok = null;

	protected Text hostText = null;

	protected Text nodeText = null;

	protected Text usernameText = null;

	protected Text passwordText = null;

	protected Text osText = null;

	protected Text endText = null;

	protected Node node;

	private final String title;

	public AbstractNodeDialog(Shell parentShell, String title) {
		super(parentShell);
		this.title = title;
		node = new Node();
	}

	protected Control createNewContents(Composite parent, String message) {
		Control contents = super.createContents(parent);
		setTitle(title);
		setMessage(message);

		/*
		 * Button ok di-hidden agar user tidak salah login.
		 */
		ok = getButton(IDialogConstants.OK_ID);
		ok.setEnabled(false);

		return contents;
	}

	protected Label createLabel(Composite parent, String text) {
		return UIHelper.createLabel(parent, text);
	}

	protected Text createText(Composite container, String label) {
		createLabel(container, label);
		return UIHelper.createText(container);
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("Monas Client " + MainView.VERSION);
	}

	protected void error(String message) {
		ok.setEnabled(false);
		setErrorMessage(message);
	}

	/**
	 * Method untuk mengecek apakah teks username dan password sudah lengkap.
	 * Bila sudah button OK akan ditampilkan.
	 */
	protected void checkCompleted() {
		if (node != null) {
			if (node.isComplete()) {
				ok.setEnabled(true);
				setErrorMessage(null);
				setMessage("Click OK to finish add node");
				return;
			}
		}

		if (ok != null) {
			error("Node informations isn't complete, please completed!");
		}
	}

	/**
	 * Ambil node dan informasinya yang diketik oleh user.
	 * 
	 * @return node yang diedit
	 */
	public Node getNode() {
		return node;
	}

}
